package com.team.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.dao.AdminMapper;
import com.team.entity.Admin;
import com.team.service.AdminService;
import com.team.util.WebUtil;
import com.team.vo.QueryAdminVo;
import com.team.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2022/10/13 20:43
 * @Description
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result addAdmin(Admin admin) {
        String account = adminMapper.selectByLast();
        admin.setAccount(account);
        int i = adminMapper.insertSelective(admin);
        return new Result(i==0?500:200,i==0?"添加失败":"添加成功");
    }

    @Override
    public Result findAll(Integer currentPage, Integer pageSize, QueryAdminVo queryAdminVo) {
        PageHelper.startPage(currentPage,pageSize);
        if(queryAdminVo.getValue() != null && queryAdminVo.getValue().length != 0){
            queryAdminVo.setStartDate(queryAdminVo.getValue()[0]);
            queryAdminVo.setEndDate(queryAdminVo.getValue()[1]);
        }
        List<Admin> admins = adminMapper.selectAllBySid(queryAdminVo);
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result deleteAdmin(Integer id) {
        int i = adminMapper.updateByStatus(id);
        return new Result(i==0?500:200,i==0?"删除失败":"删除成功");
    }

    @Override
    public Result updateAdmin(Admin admin) {
        int i = adminMapper.updateByPrimaryKeySelective(admin);
        return new Result(i==0?500:200,i==0?"修改失败":"修改成功");
    }

    @Override
    public Result selectBySectionId(int sid) {
        List<Admin> admin = adminMapper.selectBySectionId(sid);
        return new Result(200,"查询成功",admin);
    }

    @Override
    public Result selectByid(int id) {
        Admin admin = adminMapper.selectById(id);
        return new Result(200,"查询成功",admin);
    }

    @Override
    public Result selectAll() {
        List<Admin> admins =  adminMapper.selectAll();
        return new Result(200,"查询成功",admins);
    }


}
